<!DOCTYPE html>
<html lang=en>
<head>
    <!-- so meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="HandheldFriendly" content="True">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <meta name="description" content="WinDbg调试高内存的.Net进程DumpWinDbg的学习路径，艰难曲折，多次研究进展不多，今日有所进展，记录下来。 微软官方帮助文档非常全面：https:&#x2F;&#x2F;msdn.microsoft.com&#x2F;zh-cn&#x2F;library&#x2F;windows&#x2F;hardware&#x2F;ff551063(v&#x3D;vs.85).aspx 问题发现在服务器上，服务器为WinServer2012 R2 x64。其中一个Window">
<meta property="og:type" content="article">
<meta property="og:title" content="WinDbg调试高内存的.Net进程Dump">
<meta property="og:url" content="http://yoursite.com/2019/08/09/WinDbg%E8%B0%83%E8%AF%95%E9%AB%98%E5%86%85%E5%AD%98%E7%9A%84.Net%E8%BF%9B%E7%A8%8BDump/index.html">
<meta property="og:site_name" content="TmoonSite">
<meta property="og:description" content="WinDbg调试高内存的.Net进程DumpWinDbg的学习路径，艰难曲折，多次研究进展不多，今日有所进展，记录下来。 微软官方帮助文档非常全面：https:&#x2F;&#x2F;msdn.microsoft.com&#x2F;zh-cn&#x2F;library&#x2F;windows&#x2F;hardware&#x2F;ff551063(v&#x3D;vs.85).aspx 问题发现在服务器上，服务器为WinServer2012 R2 x64。其中一个Window">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://images2015.cnblogs.com/blog/347565/201705/347565-20170514100337722-171065805.png">
<meta property="og:image" content="https://images2015.cnblogs.com/blog/347565/201705/347565-20170514100647629-1672737371.png">
<meta property="og:image" content="https://images2015.cnblogs.com/blog/347565/201705/347565-20170514100741254-2114539012.png">
<meta property="article:published_time" content="2019-08-08T20:57:23.000Z">
<meta property="article:modified_time" content="2020-01-07T10:48:14.717Z">
<meta property="article:author" content="Tmoonlight">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://images2015.cnblogs.com/blog/347565/201705/347565-20170514100337722-171065805.png">
    
    
        
          
              <link rel="shortcut icon" href="/images/favicon.ico">
          
        
        
          
            <link rel="icon" type="image/png" href="/images/logo2.gif" sizes="192x192">
          
        
        
          
            <link rel="apple-touch-icon" sizes="180x180" href="/images/logo2.gif">
          
        
    
    <!-- title -->
    <title>WinDbg调试高内存的.Net进程Dump</title>
    <!-- styles -->
    
<link rel="stylesheet" href="/css/style.css">

    <!-- persian styles -->
    
      
<link rel="stylesheet" href="/css/rtl.css">

    
    <!-- rss -->
    
    
<meta name="generator" content="Hexo 4.2.0"></head>

<body class="max-width mx-auto px3 ltr">
    
      <div id="header-post">
  <a id="menu-icon" href="#"><i class="fas fa-bars fa-lg"></i></a>
  <a id="menu-icon-tablet" href="#"><i class="fas fa-bars fa-lg"></i></a>
  <a id="top-icon-tablet" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');" style="display:none;"><i class="fas fa-chevron-up fa-lg"></i></a>
  <span id="menu">
    <span id="nav">
      <ul>
         
          <li><a href="/">Home</a></li>
         
          <li><a href="/about/">About</a></li>
         
          <li><a href="/archives/">Writing</a></li>
         
          <li><a href="/projects_url">Projects</a></li>
        
      </ul>
    </span>
    <br/>
    <span id="actions">
      <ul>
        
        <li><a class="icon" href="/2019/08/10/%E9%A1%B9%E7%9B%AE%E9%85%8D%E7%BD%AE-kd/"><i class="fas fa-chevron-left" aria-hidden="true" onmouseover="$('#i-prev').toggle();" onmouseout="$('#i-prev').toggle();"></i></a></li>
        
        
        <li><a class="icon" href="/2019/08/08/VSTO%E5%BC%80%E5%8F%91%E4%B9%8B%E5%9D%91/"><i class="fas fa-chevron-right" aria-hidden="true" onmouseover="$('#i-next').toggle();" onmouseout="$('#i-next').toggle();"></i></a></li>
        
        <li><a class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up" aria-hidden="true" onmouseover="$('#i-top').toggle();" onmouseout="$('#i-top').toggle();"></i></a></li>
        <li><a class="icon" href="#"><i class="fas fa-share-alt" aria-hidden="true" onmouseover="$('#i-share').toggle();" onmouseout="$('#i-share').toggle();" onclick="$('#share').toggle();return false;"></i></a></li>
      </ul>
      <span id="i-prev" class="info" style="display:none;">Previous post</span>
      <span id="i-next" class="info" style="display:none;">Next post</span>
      <span id="i-top" class="info" style="display:none;">Back to top</span>
      <span id="i-share" class="info" style="display:none;">Share post</span>
    </span>
    <br/>
    <div id="share" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=http://yoursite.com/2019/08/09/WinDbg%E8%B0%83%E8%AF%95%E9%AB%98%E5%86%85%E5%AD%98%E7%9A%84.Net%E8%BF%9B%E7%A8%8BDump/" target="_blank" rel="noopener"><i class="fab fa-facebook " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=http://yoursite.com/2019/08/09/WinDbg%E8%B0%83%E8%AF%95%E9%AB%98%E5%86%85%E5%AD%98%E7%9A%84.Net%E8%BF%9B%E7%A8%8BDump/&text=WinDbg调试高内存的.Net进程Dump" target="_blank" rel="noopener"><i class="fab fa-twitter " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=http://yoursite.com/2019/08/09/WinDbg%E8%B0%83%E8%AF%95%E9%AB%98%E5%86%85%E5%AD%98%E7%9A%84.Net%E8%BF%9B%E7%A8%8BDump/&title=WinDbg调试高内存的.Net进程Dump" target="_blank" rel="noopener"><i class="fab fa-linkedin " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=http://yoursite.com/2019/08/09/WinDbg%E8%B0%83%E8%AF%95%E9%AB%98%E5%86%85%E5%AD%98%E7%9A%84.Net%E8%BF%9B%E7%A8%8BDump/&is_video=false&description=WinDbg调试高内存的.Net进程Dump" target="_blank" rel="noopener"><i class="fab fa-pinterest " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=WinDbg调试高内存的.Net进程Dump&body=Check out this article: http://yoursite.com/2019/08/09/WinDbg%E8%B0%83%E8%AF%95%E9%AB%98%E5%86%85%E5%AD%98%E7%9A%84.Net%E8%BF%9B%E7%A8%8BDump/"><i class="fas fa-envelope " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://getpocket.com/save?url=http://yoursite.com/2019/08/09/WinDbg%E8%B0%83%E8%AF%95%E9%AB%98%E5%86%85%E5%AD%98%E7%9A%84.Net%E8%BF%9B%E7%A8%8BDump/&title=WinDbg调试高内存的.Net进程Dump" target="_blank" rel="noopener"><i class="fab fa-get-pocket " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=http://yoursite.com/2019/08/09/WinDbg%E8%B0%83%E8%AF%95%E9%AB%98%E5%86%85%E5%AD%98%E7%9A%84.Net%E8%BF%9B%E7%A8%8BDump/&title=WinDbg调试高内存的.Net进程Dump" target="_blank" rel="noopener"><i class="fab fa-reddit " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=http://yoursite.com/2019/08/09/WinDbg%E8%B0%83%E8%AF%95%E9%AB%98%E5%86%85%E5%AD%98%E7%9A%84.Net%E8%BF%9B%E7%A8%8BDump/&title=WinDbg调试高内存的.Net进程Dump" target="_blank" rel="noopener"><i class="fab fa-stumbleupon " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=http://yoursite.com/2019/08/09/WinDbg%E8%B0%83%E8%AF%95%E9%AB%98%E5%86%85%E5%AD%98%E7%9A%84.Net%E8%BF%9B%E7%A8%8BDump/&title=WinDbg调试高内存的.Net进程Dump" target="_blank" rel="noopener"><i class="fab fa-digg " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=http://yoursite.com/2019/08/09/WinDbg%E8%B0%83%E8%AF%95%E9%AB%98%E5%86%85%E5%AD%98%E7%9A%84.Net%E8%BF%9B%E7%A8%8BDump/&name=WinDbg调试高内存的.Net进程Dump&description=" target="_blank" rel="noopener"><i class="fab fa-tumblr " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://news.ycombinator.com/submitlink?u=http://yoursite.com/2019/08/09/WinDbg%E8%B0%83%E8%AF%95%E9%AB%98%E5%86%85%E5%AD%98%E7%9A%84.Net%E8%BF%9B%E7%A8%8BDump/&t=WinDbg调试高内存的.Net进程Dump" target="_blank" rel="noopener"><i class="fab fa-hacker-news " aria-hidden="true"></i></a></li>
</ul>

    </div>
    <div id="toc">
      <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#WinDbg调试高内存的-Net进程Dump"><span class="toc-number">1.</span> <span class="toc-text">WinDbg调试高内存的.Net进程Dump</span></a></li></ol>
    </div>
  </span>
</div>

    
    <div class="content index py4">
        
        <article class="post" itemscope itemtype="http://schema.org/BlogPosting">
  <header>
    
    <h1 class="posttitle" itemprop="name headline">
        WinDbg调试高内存的.Net进程Dump
    </h1>



    <div class="meta">
      <span class="author" itemprop="author" itemscope itemtype="http://schema.org/Person">
        <span itemprop="name">TmoonSite</span>
      </span>
      
    <div class="postdate">
      
        <time datetime="2019-08-08T20:57:23.000Z" itemprop="datePublished">2019-08-09</time>
        
      
    </div>


      

      

    </div>
  </header>
  

  <div class="content" itemprop="articleBody">
    <h1 id="WinDbg调试高内存的-Net进程Dump"><a href="#WinDbg调试高内存的-Net进程Dump" class="headerlink" title="WinDbg调试高内存的.Net进程Dump"></a><a href="https://www.cnblogs.com/wigis/p/6851918.html" target="_blank" rel="noopener">WinDbg调试高内存的.Net进程Dump</a></h1><p>WinDbg的学习路径，艰难曲折，多次研究进展不多，今日有所进展，记录下来。</p>
<p>微软官方帮助文档非常全面：<a href="https://msdn.microsoft.com/zh-cn/library/windows/hardware/ff551063(v=vs.85).aspx" target="_blank" rel="noopener">https://msdn.microsoft.com/zh-cn/library/windows/hardware/ff551063(v=vs.85).aspx</a></p>
<p>问题发现在服务器上，服务器为WinServer2012 R2 x64。其中一个Windows服务，内存高达7G。但此服务，无什么操作，仅仅定时获取数据，更新数据。使用的EntityFramework。用任务管理器，抓包下来，查看。Dump包有7GB之大。</p>
<p>1、准备环境，加载sos.dll===========================</p>
<p>开始调试，首先WinDbg，分为x64和x86版本。由于Dump运行环境，为64位，故WinDbg也应64位，这就要主机调试环境也应64位。我主机环境为win8.1x64。</p>
<p>运行WinDbg，一定要管理员权限。</p>
<p>然后，确认是否已加载sos.dll。使用.chain命令，查看。若未加载，则加载64位的sos.dll。因为Dump是x64位的，故加载x32位不成，会提示</p>
<p>“The call to LoadLibrary(C:\Windows[Microsoft.NET](<a href="http://microsoft.net/)\Framework\v4.0.30319\sos.dll" target="_blank" rel="noopener">http://microsoft.net/)\Framework\v4.0.30319\sos.dll</a>) failed, Win32 error 0n193</p>
<p>“%1 不是有效的 Win32 应用程序。””</p>
<p>sos.dll微软官方帮助文档，<a href="https://msdn.microsoft.com/en-us/library/bb190764(v=vs.110).aspx" target="_blank" rel="noopener">https://msdn.microsoft.com/en-us/library/bb190764(v=vs.110).aspx</a></p>
<p>2、调试===================================</p>
<p>1）、.cls清屏。</p>
<p>2）、!eeheap -gc 查看托管堆，发现2代堆非常大，近2.5G。故确定问题在此。</p>
<p>每个Segment最大255M，begin为起始地址，allocated为结束地址，结束地址减去起始地址等于size,括号里为10进制大小。LOB并不大。</p>
<p> <img src="https://images2015.cnblogs.com/blog/347565/201705/347565-20170514100337722-171065805.png" alt=""> </p>
<p>3）、由于dump很大，全分析很慢，故只取一个二代堆的Segment分析。!DumpHeap 000000008f591000  000000009f58ffe0 </p>
<p>4)、分析几分钟后，stat统计节显示String类型最大。故再次分析String类型。!DumpHeap -type String  000000008f591000  000000009f58ffe0 </p>
<p>5）、发现String类型，有很多4KB字符串，不知什么。进一步分析大字符串，!DumpHeap -type String -min 1000  000000008f591000  000000009f58ffe0 </p>
<p>6）、!do 查看对象，发现String是错误信息。联系之前，此系统确实一直出现报错行为，但不影响使用也就没管。</p>
<p><img src="https://images2015.cnblogs.com/blog/347565/201705/347565-20170514100647629-1672737371.png" alt=""></p>
<p>7）、!gcroot -all 查看引用 ，此操作也比较慢。发现String是一个对象属性，而对象是EF5的Context表记录。</p>
<p>8)、重复6）、7）步骤，发现都是如此。那么这个Context一直有效，其中内容也就一直被引用。</p>
<p><img src="https://images2015.cnblogs.com/blog/347565/201705/347565-20170514100741254-2114539012.png" alt=""></p>
<p>8）、这时，用IL查看代码，发现确实有个静态的EF的ObjectContext被引用，此对象生命期与进程一致。由于长期运行，加入数据，ObjectContext会一直增加。</p>
<p>关于这个问题，我找了下EF相关文档，似乎没有清理DBContext的命令。</p>
<p>其实，这种操作非常常见，为此，我单独做了测试程序，证明了以上结论。 </p>
<p><a href="https://www.cnblogs.com/wigis/p/6851918.html#" target="_blank" rel="noopener">+ View Code</a></p>
<p>　　</p>
<p>3、帮助信息================</p>
<p>1)、WinDbg可使用!help cmd.获取帮助，比如!help dumpheap</p>
<p>2)、可用搜索内存s -u 0x017a1000 0x017c8c78 “朝生暮死”</p>
<p>或搜索内存到底 .foreach (addr { s -[1]u 0x017a1000 0x017c8c78 “朝生暮死”}){du ${addr}}</p>
<p>3）、</p>

  </div>
</article>



        
          <div id="footer-post-container">
  <div id="footer-post">

    <div id="nav-footer" style="display: none">
      <ul>
         
          <li><a href="/">Home</a></li>
         
          <li><a href="/about/">About</a></li>
         
          <li><a href="/archives/">Writing</a></li>
         
          <li><a href="/projects_url">Projects</a></li>
        
      </ul>
    </div>

    <div id="toc-footer" style="display: none">
      <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#WinDbg调试高内存的-Net进程Dump"><span class="toc-number">1.</span> <span class="toc-text">WinDbg调试高内存的.Net进程Dump</span></a></li></ol>
    </div>

    <div id="share-footer" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=http://yoursite.com/2019/08/09/WinDbg%E8%B0%83%E8%AF%95%E9%AB%98%E5%86%85%E5%AD%98%E7%9A%84.Net%E8%BF%9B%E7%A8%8BDump/" target="_blank" rel="noopener"><i class="fab fa-facebook fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=http://yoursite.com/2019/08/09/WinDbg%E8%B0%83%E8%AF%95%E9%AB%98%E5%86%85%E5%AD%98%E7%9A%84.Net%E8%BF%9B%E7%A8%8BDump/&text=WinDbg调试高内存的.Net进程Dump" target="_blank" rel="noopener"><i class="fab fa-twitter fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=http://yoursite.com/2019/08/09/WinDbg%E8%B0%83%E8%AF%95%E9%AB%98%E5%86%85%E5%AD%98%E7%9A%84.Net%E8%BF%9B%E7%A8%8BDump/&title=WinDbg调试高内存的.Net进程Dump" target="_blank" rel="noopener"><i class="fab fa-linkedin fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=http://yoursite.com/2019/08/09/WinDbg%E8%B0%83%E8%AF%95%E9%AB%98%E5%86%85%E5%AD%98%E7%9A%84.Net%E8%BF%9B%E7%A8%8BDump/&is_video=false&description=WinDbg调试高内存的.Net进程Dump" target="_blank" rel="noopener"><i class="fab fa-pinterest fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=WinDbg调试高内存的.Net进程Dump&body=Check out this article: http://yoursite.com/2019/08/09/WinDbg%E8%B0%83%E8%AF%95%E9%AB%98%E5%86%85%E5%AD%98%E7%9A%84.Net%E8%BF%9B%E7%A8%8BDump/"><i class="fas fa-envelope fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://getpocket.com/save?url=http://yoursite.com/2019/08/09/WinDbg%E8%B0%83%E8%AF%95%E9%AB%98%E5%86%85%E5%AD%98%E7%9A%84.Net%E8%BF%9B%E7%A8%8BDump/&title=WinDbg调试高内存的.Net进程Dump" target="_blank" rel="noopener"><i class="fab fa-get-pocket fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=http://yoursite.com/2019/08/09/WinDbg%E8%B0%83%E8%AF%95%E9%AB%98%E5%86%85%E5%AD%98%E7%9A%84.Net%E8%BF%9B%E7%A8%8BDump/&title=WinDbg调试高内存的.Net进程Dump" target="_blank" rel="noopener"><i class="fab fa-reddit fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=http://yoursite.com/2019/08/09/WinDbg%E8%B0%83%E8%AF%95%E9%AB%98%E5%86%85%E5%AD%98%E7%9A%84.Net%E8%BF%9B%E7%A8%8BDump/&title=WinDbg调试高内存的.Net进程Dump" target="_blank" rel="noopener"><i class="fab fa-stumbleupon fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=http://yoursite.com/2019/08/09/WinDbg%E8%B0%83%E8%AF%95%E9%AB%98%E5%86%85%E5%AD%98%E7%9A%84.Net%E8%BF%9B%E7%A8%8BDump/&title=WinDbg调试高内存的.Net进程Dump" target="_blank" rel="noopener"><i class="fab fa-digg fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=http://yoursite.com/2019/08/09/WinDbg%E8%B0%83%E8%AF%95%E9%AB%98%E5%86%85%E5%AD%98%E7%9A%84.Net%E8%BF%9B%E7%A8%8BDump/&name=WinDbg调试高内存的.Net进程Dump&description=" target="_blank" rel="noopener"><i class="fab fa-tumblr fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://news.ycombinator.com/submitlink?u=http://yoursite.com/2019/08/09/WinDbg%E8%B0%83%E8%AF%95%E9%AB%98%E5%86%85%E5%AD%98%E7%9A%84.Net%E8%BF%9B%E7%A8%8BDump/&t=WinDbg调试高内存的.Net进程Dump" target="_blank" rel="noopener"><i class="fab fa-hacker-news fa-lg" aria-hidden="true"></i></a></li>
</ul>

    </div>

    <div id="actions-footer">
        <a id="menu" class="icon" href="#" onclick="$('#nav-footer').toggle();return false;"><i class="fas fa-bars fa-lg" aria-hidden="true"></i> Menu</a>
        <a id="toc" class="icon" href="#" onclick="$('#toc-footer').toggle();return false;"><i class="fas fa-list fa-lg" aria-hidden="true"></i> TOC</a>
        <a id="share" class="icon" href="#" onclick="$('#share-footer').toggle();return false;"><i class="fas fa-share-alt fa-lg" aria-hidden="true"></i> Share</a>
        <a id="top" style="display:none" class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up fa-lg" aria-hidden="true"></i> Top</a>
    </div>

  </div>
</div>

        
        <footer id="footer">
  <div class="footer-left">
    Copyright &copy; 2020 Tmoonlight
  </div>
  <div class="footer-right">
    <nav>
      <ul>
         
          <li><a href="/">Home</a></li>
         
          <li><a href="/about/">About</a></li>
         
          <li><a href="/archives/">Writing</a></li>
         
          <li><a href="/projects_url">Projects</a></li>
        
      </ul>
    </nav>
  </div>
</footer>

    </div>
    <!-- styles -->

<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">


<link rel="stylesheet" href="/lib/justified-gallery/css/justifiedGallery.min.css">


    <!-- jquery -->

<script src="/lib/jquery/jquery.min.js"></script>


<script src="/lib/justified-gallery/js/jquery.justifiedGallery.min.js"></script>

<!-- clipboard -->

  
<script src="/lib/clipboard/clipboard.min.js"></script>

  <script type="text/javascript">
  $(function() {
    // copy-btn HTML
    var btn = "<span class=\"btn-copy tooltipped tooltipped-sw\" aria-label=\"Copy to clipboard!\">";
    btn += '<i class="far fa-clone"></i>';
    btn += '</span>'; 
    // mount it!
    $(".highlight table").before(btn);
    var clip = new ClipboardJS('.btn-copy', {
      text: function(trigger) {
        return Array.from(trigger.nextElementSibling.querySelectorAll('.code')).reduce((str,it)=>str+it.innerText+'\n','')
      }
    });
    clip.on('success', function(e) {
      e.trigger.setAttribute('aria-label', "Copied!");
      e.clearSelection();
    })
  })
  </script>


<script src="/js/main.js"></script>

<!-- search -->

<!-- Google Analytics -->

<!-- Baidu Analytics -->

<!-- Disqus Comments -->


</body>
</html>
